<template>
    <div>
        <rui-page :defines="defines">
            <rui-query :label-width="180" ref="searchForm" :query="queryData" :query-rows="searchFormOptions"
                       :more-rows="searchFormOptionsRows">
                <template slot="createTimes" slot-scope="{ key,data }">
                    <DatePicker
                        v-model="data[key]"
                        type="daterange"
                        format="yyyy-MM-dd"
                        clearable
                        @on-change="selectCreateDateRange"
                        placeholder="选择起始时间"
                        style="width: 200px"
                    ></DatePicker>
                </template>
                <template slot="endDates" slot-scope="{ key,data }">
                    <DatePicker
                        v-model="data[key]"
                        type="daterange"
                        format="yyyy-MM-dd"
                        clearable
                        @on-change="selectEndDateRange"
                        placeholder="选择起始时间"
                        style="width: 200px"
                    ></DatePicker>
                </template>
            </rui-query>
            <rui-table
                :defineId="'penaltyExemptDefine'"
                :fixedLeft="['action']"
                :showIndex="false"
                :select="false"
                :columns="overpaymentDifferenceColumns"
                :slots="[{key:'action',slot:'action'},{key: 'contractNo',slot: 'contractNo'}]"
                @loadDatas="queryData"
                ref-query="searchForm"
                ref="penaltyExemptTable"
            >
                <template slot="toolBar">
                    <Button @click="showModal" icon="md-add" type="primary">新增申请</Button>
                    <!--                    <Button type="primary">导出报表</Button>-->
                </template>
                <template slot="action" slot-scope="{ row }">
                    <div>
                        <!--
                            处理  isViewBtn ~~审核中 待补件 待补件审核~~
                            编辑 草稿
                            删除 草稿
                        -->
                        <Button :disabled="!row.isViewBtn" type="primary" size="small"
                                @click="edit('process',row)">处理
                        </Button>

                        <Button :disabled="row.processStatus !== 'draft'" type="primary" size="small"
                                @click="edit('write',row)">编辑
                        </Button>

                        <Button :disabled="row.processStatus !== 'draft'" type="error" size="small"
                                @click="remove(row)">
                            删除
                        </Button>
                    </div>
                </template>
                <template slot="contractNo" slot-scope="{ row }">
                    <a @click="edit('read',row)">{{row.contractNo}}</a>
                </template>
            </rui-table>
            <ActivatedCase
                :isSP="isSP"
                :getDataFn="getDataFn"
                :contractBusiness="contractBusiness"
                :handleBusiness="'exemptTypes'"
                :dataDict="dataDict"
                :modal="modal"
                :modalTitle="modalTitle"
                @showContractDetial="showContractDetial"
                @closeModal="closeModal"
            />
        </rui-page>
    </div>
</template>
<script>
    import {mapGetters} from "vuex";
    import penaltyExemptDefine from "./penalty-exempt-define";
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    import ActivatedCase from "_p/basic/pages/contract/common/activated-case/index";
    import {channelType, roleType} from "@/projects/basic/api/contract/channel-type/channel-type";
    import {
        findPenaltyExemptList,
        getBasicCostList,
        delById
    } from "_p/basic/api/contract/fee-waiver/penalty-exempt/penalty-exempt";

    export default {
        components: {
            ActivatedCase,
        },
        data() {
            return {
                pageNo: 1,
                Obj: {},
                // 弹窗状态
                modalTitle: "",
                contractBusiness: ["exemptTypes", "costExemptBill", "closeContract"],
                modal: false,
                dicttArr: ["PersonRoleType", "contractStatus", "channelType"],
                // selct 数据字典
                dataDict: {},
                searchFormOptions: [
                    {
                        defineId: "penaltyExemptDefine",
                        fields: ["applyNo", "contractNo", "custName"]
                    }
                ],
                searchFormOptionsRows: [
                    {
                        defineId: "penaltyExemptDefine",
                        fields: ["createTimes", "endDates", "processStatus"],
                        slots: [{key: 'createTimes', slotName: 'createTimes'},
                            {key: 'endDates', slotName: 'endDates'}
                        ]
                    }
                ],
                overpaymentDifferenceColumns: [
                    'action', 'applyNo', 'caseNo', 'contractNo', 'custName', 'processStatus', 'totalPenalty', 'penaltyAmt',
                    'reductionRatio', 'exemptionAmt', 'overdueAmt', 'applyDate', 'endDate'],
            };
        },
        computed: {
            ...mapGetters({
                userInfo: "userInfo",
                roleType: "roles",
            }),
            defines() {
                return [
                    {
                        id: "penaltyExemptDefine",
                        fields: penaltyExemptDefine
                    }
                ];
            },
            // 是否是合作商
            isSP: function () {
                return channelType(this.roleType)
                // return true;
            },
            // 是否是风险
            isRisk: function () {
                return roleType(this.roleType)
                // return true;
            },
            getDataFn: function () {
                return getBasicCostList;
            }
        },
        mounted() {
            // 数据字典
            this.getDictDataList();
            // this.refresh();
        },
        activated() {
            this.refresh();
        },
        methods: {
            selectCreateDateRange(v) {
                if (v) {
                    this.$refs.searchForm.setFiledValue("startCreateDate", v[0]);
                    this.$refs.searchForm.setFiledValue("endCreateDate", v[1]);
                }
            },
            selectEndDateRange(v) {
                if (v) {
                    this.$refs.searchForm.setFiledValue("startFinishDate", v[0]);
                    this.$refs.searchForm.setFiledValue("endFinishDate", v[1]);
                }
            },
            queryData(queryData) {
                findPenaltyExemptList(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.penaltyExemptTable.updateTableData(records, total);
                    }
                });
            },
            refresh() {
                this.$refs['penaltyExemptTable'].reloadData();
            },
            getDictDataList() {
                getDictDataList(this.dicttArr).then(res => {
                    this.dataDict = res;
                });
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除单号 " + v.applyNo + " ?",
                    onOk: () => {
                        delById(v.id).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success(res.msg);
                                this.refresh();
                            }
                        });
                    }
                });
            },
            showModal() {
                this.modalTitle = "已激活案件列表";
                this.modal = true;
            },
            closeModal() {
                this.modal = false;
            },
            showContractDetial(params) {
                params.editFlag = false;
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/fee-waiver/penalty-exempt-detial/index",
                    "豁免罚息详情",
                    "ios-add",
                    params,
                    "penalty-exempt-detial-" + this.pageNo,
                    [],
                    true
                );
                this.pageNo += 1;
            },
            edit(type, params) {
                params.readOnly = type;
                params.editFlag = true;
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/fee-waiver/penalty-exempt-detial/index",
                    "豁免罚息详情",
                    "ios-add",
                    params,
                    "penalty-exempt-detial-" + this.pageNo,
                    [],
                    true
                );
                this.pageNo += 1;
            }
        }
    };
</script>
